/*
 * @lc app=leetcode id=122 lang=cpp
 *
 * [122] Best Time to Buy and Sell Stock II
 */
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len = prices.size();
        if (len < 2) {
            return 0;
        }
        int profit = 0;
        int maxProfit = 0;
        int minPrice = prices[0];
        int maxPrice = prices[0];

        for (int i=1; i<len; i++) {

            if (prices[i] > maxPrice) {
                maxPrice = prices[i];
            } else {
                maxProfit += maxPrice - minPrice;
                maxPrice = prices[i];
                minPrice = prices[i];
            }

            if (prices[i] < minPrice) {
                minPrice = prices[i];
            }
        }

        maxProfit += maxPrice - minPrice;
        return maxProfit;
    }
};

